// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

import 'package:junyeyishu/widgets/QGWidget.dart';
import 'package:permission_handler/permission_handler.dart';
import '/utils/utils.dart';

class QGPayWebView extends StatefulWidget {
  String? url;
  QGPayWebView({super.key, this.url});
  @override
  State<QGPayWebView> createState() => _QGPayWebViewState();
}

class _QGPayWebViewState extends State<QGPayWebView> {
  String? title;
 late InAppWebViewController _webViewController;
  bool loading = true;

   fallbackHandler(dd){
       Navigator.of(context).pop();
              eventBus.fire(EventMainTabSelectIndex(index: 3));
              Navigator.of(context).pushReplacementNamed(PageMyHomeOrder);
   }
 
  @override
  Widget build(BuildContext context) {
    print('objectffffff');
    return QGScaffold(
      title: title ?? "",
      bgColor: Color(0xFFFFFFFF),
      body: PopScope(
        canPop: true, 
       
       onPopInvokedWithResult: (bool didPop,dynamic) async {
        // return await _handleSystemBack();
      },

        child: InAppWebView(
          initialUrlRequest: URLRequest(url: WebUri(widget.url ?? "")),
          onWebViewCreated: (controller) async{
             _webViewController = controller;


            // controller.addJavaScriptHandler(
            //     handlerName: 'fromWeb', 
            //     callback: (args) {
            //       print('收到Web数据: ${args[0]}');
            //     }
            //   );

          },
          onLoadStop: (controller, url) async {
                
          },
          onLoadStart: (controller, url) async{
                     
      

            // 根据不同的返回地址，跳到到不同的界面
            if (url.toString() == '${Network.h5URL}payFee.html') {
              fallbackHandler(1);
              // Navigator.of(context).pop();
              // Navigator.of(context).pushReplacementNamed(PageSelectWallet);
            }

            if (url.toString() == '${Network.h5URL}payOrder.html') {
              Navigator.of(context).pop();
              eventBus.fire(EventMainTabSelectIndex(index: 3));
              Navigator.of(context).pushReplacementNamed(PageMyHomeOrder);
            }

            if (url.toString() == "${Network.h5URL}marketOrder.html") {
               fallbackHandler(1);
              // Navigator.of(context).pop();
              // Navigator.of(context).pop();
              // eventBus.fire(EventMainTabSelectIndex(index: 3));
              // Navigator.of(context).pushNamed(PageMyOrder);
            }

            if (url.toString() == '${Network.h5URL}batchOrder.html') {
               fallbackHandler(1);
              // Navigator.of(context).pop();
              // Navigator.of(context).pop();
              // eventBus.fire(EventMainTabSelectIndex(index: 3));
              // Navigator.of(context).pushReplacementNamed(PageMyBatchOrder);
            }

            if (url.toString() == '${Network.h5URL}paySurety.html') {

               fallbackHandler(1);
              // Navigator.of(context).pop();
            }

            if (url.toString() == '${Network.h5URL}payPurchase.html') {
               fallbackHandler(1);
               
              // Navigator.of(context).pop();
              // Navigator.of(context).pop();
              // eventBus.fire(EventMainTabSelectIndex(index: 3));
              // Navigator.of(context).pushNamed(PageMyPurchaseOrder);
            }
          },
         
          // initialOptions: InAppWebViewGroupOptions(
          //   crossPlatform: InAppWebViewOptions(
          //     supportZoom: false,
          //     javaScriptEnabled: true,
          //     disableHorizontalScroll: true,
          //   ),
          // ),
          // androidOnPermissionRequest: (InAppWebViewController controller,
          //     String origin, List<String> resources) async {
          //   return PermissionRequestResponse(
          //       resources: resources,
          //       action: PermissionRequestResponseAction.GRANT);
          // }),
          initialSettings: InAppWebViewSettings(
              supportZoom: false,
              javaScriptEnabled: true,
              disableHorizontalScroll: false,
              disableVerticalScroll: false,
              horizontalScrollBarEnabled: false,
              verticalScrollBarEnabled: false,
              mediaPlaybackRequiresUserGesture: false,
              allowsInlineMediaPlayback: true,
              javaScriptCanOpenWindowsAutomatically: true,
              transparentBackground: false,
              useHybridComposition: true,
          ),
                 onReceivedServerTrustAuthRequest:
                  (controller, challenge) async {
                // 允许不安全的 HTTPS 链接
                return ServerTrustAuthResponse(
                    action: ServerTrustAuthResponseAction.PROCEED);
              },
          onPermissionRequest: (controller, permissionRequest) async {
            //  if (!kIsWeb) {
            //    if (Platform.isAndroid) {
            //     return PermissionResponse(action: PermissionResponseAction.GRANT);
            //   }
            //  }
            return PermissionResponse(action: PermissionResponseAction.DENY);
          }
          ),
      ),
     
    );
  }
}
